package com.ht.api.service;

import cn.hutool.core.util.StrUtil;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;

import javax.sql.DataSource;

public class PlanService {

    public static void formatPlan (DataSource ds, Entity plan) {
        String sourceText = "";
        String sourceType = plan.getStr("source_type");
        String sourceId = plan.getStr("source_id");

        if (StrUtil.isBlank(sourceId)) {
            return;
        }

        if (sourceType.equals("user")) {
            String userName = Db.createSqlParser("sys_user").select("nick_name").where("main_id", sourceId).val();
            sourceText = "由 [" + userName + "] 创建";

        } else if (sourceType.equals("event")) {

            Entity adPlan = Db.createSqlParser("event_ad_plan_config").select("config_id").where("main_id", sourceId).findOne(ds);
            if (adPlan == null) {
                return;
            }

            Entity config = Db.createSqlParser("event_ad_config").where("main_id", adPlan.getStr("config_id")).findOne(ds);

            String relationId = config.getStr("relation_id");
            String relationType = config.getStr("relation_type");
            String relationIdType = config.getStr("relation_id_type");

            if (StrUtil.isBlank(relationType) || StrUtil.isBlank(relationIdType)) {
                return;
            }

            if (StrUtil.isBlank(relationId)) {
                String eventTypeName = Db.createSqlParser("sys_event_type").select("name").where("event_type", adPlan.getStr("event_type")).val();
                String eventKeyName = Db.createSqlParser("sys_event_type").select("event_name").where("event_key", adPlan.getStr("event_key")).val();

                sourceText = "来源全局配置 [" + eventTypeName + eventKeyName + "]";
            } else {
                String relationName = "";
                String relationTypeName = "";
                String relationNameCode = "";

                switch (relationType) {
                    case "ad":
                        relationName = "广告";
                        relationNameCode = "name";
                        break;
                    case "product":
                        relationName = "商品";
                        relationNameCode = "name";

                        break;
                    case "wx_workuser":
                        relationName = "客服";
                        relationNameCode = "name";
                        break;
                    case "wx_groupchat":
                        relationName = "社群";
                        relationNameCode = "name";
                        break;
                }


                StringBuilder sql = new StringBuilder();

                if (relationIdType.equals("atom")) {
                    sql.append("select "+relationNameCode+" from ").append(relationIdType).append(" where main_id = ? ");
                } else if (relationIdType.equals("group")) {
                    sql.append("select group_name from group where main_id = ? ");
                    relationTypeName = "分组";
                } else if (relationIdType.equals("tag")) {
                    sql.append("select tag_name from tag where main_id = ? ");
                    relationTypeName = "标签";
                }

                String idName = Db.val(ds, sql.toString(), relationId);

                sourceText = "来源" + relationName + relationTypeName + " ["+idName+"]";
            }

        } else if (sourceType.equals("sop")) {
            Entity stage = Db.createSqlParser("stage").where("main_id", sourceId).findOne(ds);
            if (stage == null) {
                return;
            }
            String sopName = Db.createSqlParser("sop").select("name").where("main_id", stage.getStr("sop_id")).val(ds);

            sourceText = "来源 旅程[" + sopName + "], 阶段[" + stage.getStr("name") + "]";
        }

        plan.set("source_text", sourceText);
    }




}
